package com.help10000.rms.ui.adapter

import android.content.Context
import android.content.Intent
import android.view.View
import android.view.ViewGroup
import com.amir.common.beans.AddressItem
import com.amir.common.holder.CommonViewHolder
import com.amir.common.mvp.base.BaseRecyclerViewAdapter
import com.help10000.rms.R
import com.help10000.rms.mvp.presenter.AddressListPresenter
import com.help10000.rms.ui.activitys.mine.address.AddAddressActivity
import com.lxj.xpopup.XPopup

class AddressListAdapter(
    context: Context?,
    dataList: List<AddressItem>,
    presenter: AddressListPresenter
) :
    BaseRecyclerViewAdapter<AddressItem>(context, dataList) {

    private var mPresenter = presenter
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CommonViewHolder {
        var view = layoutInflater.inflate(R.layout.item_address_item, parent, false)
        return CommonViewHolder(view)
    }

    override fun onBindViewHolder(holder: CommonViewHolder, position: Int) {
        var addressItem = dataList[position]
        holder.setText(
            R.id.address,
            text = "${addressItem.ProvName}${addressItem.CityName}${addressItem.TownName}${addressItem.Zone5}"
        )
        holder.setText(R.id.Mobile, text = "${addressItem.Mobile}")
        holder.setText(R.id.ContactName, text = "${addressItem.ContactName}")
        if (addressItem.IsDefault) {
            holder.setViewVisibility(R.id.IsDefault, View.VISIBLE)
            holder.setViewVisibility(R.id.btnDefault, View.GONE)
        } else {
            holder.setViewVisibility(R.id.IsDefault, View.GONE)
            holder.setViewVisibility(R.id.btnDefault, View.VISIBLE)
        }
        holder.setViewVisibility(
            R.id.IsDefault,
            if (addressItem.IsDefault) View.VISIBLE else View.GONE
        )

        holder.setOnClickListener(R.id.btnDefault)
        {
            mPresenter.setDefault(addressItem.AddrId)
        }
        holder.setOnClickListener(R.id.btnDelete)
        {
            XPopup.Builder(mContext)
                .asConfirm(
                    null, "确定删除该条地址吗？"
                ) {
                    mPresenter.deleteAddress(addressItem.AddrId)
                }.show()

        }
        holder.setOnClickListener(R.id.btnEdit) {
            var intent = Intent(mContext, AddAddressActivity::class.java)
            intent.putExtra("address", addressItem)
            mContext.startActivity(intent)
        }
        holder.setCommonClickListener(object : CommonViewHolder.onItemCommonClickListener {
            override fun onItemClickListener(position: Int) {
                var intent =Intent(
                    mContext,AddAddressActivity::class.java
                )
                intent.putExtra("address", addressItem)
                mContext.startActivity(intent)
            }
            override fun onItemLongClickListener(position: Int) {

            }
        })
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

}
